﻿@model ProductDetailsModel

@if (Model.ProductWarehouses.Any())
{
    <div>
        <label for="warehouseId" class="col-form-label">@T("Products.Warehouse"):</label>
        <select class="custom-select form-control" name="WarehouseId" id="WarehouseId">
            @foreach (var warehouse in Model.ProductWarehouses)
            {
                <option selected="@(warehouse.Selected)" value="@warehouse.WarehouseId">@(warehouse.Name)</option>
            }
        </select>
    </div>
    @if (Model.ProductAttributes.Any())
    {
        <script asp-location="Footer">
        @{
            var attributeChangeHandlerFuncName = string.Format("attribute_change_handler_{0}();", Model.Id);
        }
        $(document).ready(function () {
            $('#WarehouseId').change(function () { @attributeChangeHandlerFuncName });
        });
        </script>
    }
    else
    {
        <script asp-location="Footer">
            function warehouse_change_handler() {
                $.ajax({
                    cache: false,
                    url: '@Html.Raw(Url.Action("productdetails_warehousechange", "product", new { productId = Model.Id }))',
                    data: { warehouseId: $('#WarehouseId').val() },
                    type: 'post',
                    context: { product: '@Model.Id' },
                    success: function (data, url) {
                        if (data.stockAvailability) {
                            $('#stock-availability-value-@Model.Id').text(data.stockAvailability);
                        }
                    }
                });
            }
            $(document).ready(function () {
                $('#WarehouseId').on('change', function() {
                    warehouse_change_handler();
                });
                warehouse_change_handler();
            });
        </script>
    }
}
